// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CLOUDAPPENGINE_CAEHTTPS_H
#define CLOUDAPPENGINE_CAEHTTPS_H

#include <cstdio>
#include <string>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include "../libs/openssl/include/openssl/ssl.h"
#include "../libs/openssl/include/openssl/ossl_typ.h"
#include "../libs/curl/include/curl.h"
#include "CaeDefine.h"
#include "CaeLogConfig.h"
#include "CaeConfigManage.h"
#include "CaeRequestParams.h"
#include "CaeSignUtil.h"

constexpr int BUF_SIZE = 1024;

struct HttpsResponse {
    long httpCode = 0L;
    char *buffer = nullptr;
    int size = 0;
};

class CaeHttps {
public:
    CaeHttps();

    virtual ~CaeHttps();

    int Init();

    bool HttpsDownload(CaeRequestParams *request, const std::string &outputFile);

    bool HttpsPost(CaeRequestParams *request, HttpsResponse &response, time_t validDateTime);

    static size_t ResponseCallback(const void *contents, size_t size, size_t memBlockNum, void *sourcePtr);

    static long m_httpCode;

private:
    CaeSignUtil *m_sha256withRSASigner;
    void CloseHttpsClient() noexcept;
};

#endif // CLOUDAPPENGINE_CAEHTTPS_H
